// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.migration;

import java.util.ArrayList;
import java.util.List;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.ExceptionOperation;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-8 qiang.zhang $
 * 
 */
public abstract class AbstractMigrationService {

    private final static String EXTENSION_ID = "org.dengues.core.migrationService";

    private final static String ATTRI_ID = "id";

    private final static String ATTRI_NAME = "name";

    private final static String ATTRI_DESC = "description";

    private final static String ATTRI_CLASS = "class";

    private final static String ATTRI_IMAGE = "image";

    private Image image;

    private String id;

    private String name;

    private String description;

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getAllMigrations".
     * 
     * @return
     */
    public static List<AbstractMigrationService> getAllMigrations() {
        List<AbstractMigrationService> list = new ArrayList<AbstractMigrationService>();
        IConfigurationElement[] cef = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);
        for (IConfigurationElement ce : cef) {
            try {
                Object cee = ce.createExecutableExtension(ATTRI_CLASS);
                if (cee instanceof AbstractMigrationService) {
                    AbstractMigrationService ms = (AbstractMigrationService) cee;
                    ms.description = ce.getAttribute(ATTRI_DESC);
                    ms.id = ce.getAttribute(ATTRI_ID);
                    ms.name = ce.getAttribute(ATTRI_NAME);
                    String imgPath = ce.getAttribute(ATTRI_IMAGE);
                    String pid = ce.getNamespaceIdentifier();
                    Platform.getBundle(pid).getResource(imgPath);
                    ImageDescriptor imgDesc = DenguesCorePlugin.imageDescriptorFromPlugin(pid, imgPath);
                    ms.image = imgDesc.createImage();
                    list.add(ms);
                }
            } catch (CoreException e) {
                ExceptionOperation.operate(e);
            }
        }
        return list;
    }

    /**
     * Getter for id.
     * 
     * @return the id
     */
    public String getId() {
        return this.id;
    }

    /**
     * Getter for name.
     * 
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Getter for description.
     * 
     * @return the description
     */
    public String getDescription() {
        return this.description;
    }

    public abstract MigrationExecResult executeMigration();

    /**
     * Getter for image.
     * 
     * @return the image
     */
    public Image getImage() {
        return this.image;
    }
}
